package action;

import java.io.IOException;
import java.util.logging.Logger;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class CharacterEncodingFilter implements Filter {
	
	private boolean ignore;
	private String charset;
	private static final Logger LOGGER = Logger.getLogger(CharacterEncodingFilter.class.getName());

    public CharacterEncodingFilter() {
    }
    
	public void init(FilterConfig fConfig) throws ServletException {
		this.ignore = Boolean.valueOf(fConfig.getInitParameter("ignore"));
		this.charset = fConfig.getInitParameter("charset");
	}
	
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		if (this.ignore && this.charset != null) {
			request.setCharacterEncoding(this.charset);
			response.setCharacterEncoding(this.charset);
			response.setContentType("text/html; charset=" + this.charset);
			LOGGER.info("==字符编码过滤==");
		}
		chain.doFilter(request, response);
	}
	
	public void destroy() {
		this.ignore = false;
		this.charset = null;
	}

}
